/**
 * 
 */
package com.my.test.testcomponent;

import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * create date: 2010-10-20 ����09:12:29
 * @author Angela Tao
 * @version 1.0
 *
 */
public class NotePad implements ActionListener{
	
	private Frame f = new Frame("���±�");
	private TextArea ta = new TextArea();
	
	private MenuBar mb = new MenuBar();
	private Menu m = new Menu("�ļ�");
	private MenuItem mi1 = new MenuItem("��");
	private MenuItem mi2 = new MenuItem("����");
	private MenuItem mi3 = new MenuItem("�˳�");
	
	/**
	 * 
	 */
	public NotePad() {
		// TODO Auto-generated constructor stub
		ta.setFont(new Font("Arial",Font.PLAIN,16));
		f.add(ta);
		f.setMenuBar(mb);
		mb.add(m);
		m.add(mi1);
		m.add(mi2);
		m.addSeparator();
		m.add(mi3);
		
		f.setLocation(200, 200);
		f.setSize(600, 600);
		f.setVisible(true);
		
		mi1.setActionCommand("open");
		mi2.setActionCommand("save");
		mi3.setActionCommand("quit");
		
		mi1.addActionListener(this);
		mi2.addActionListener(this);
		mi3.addActionListener(this);
		
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new NotePad();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String s = arg0.getActionCommand();
		if("open".equals(s)){
			this.readFile();
		}else if("save".equals(s)){
			this.saveFile();
		}else if("quit".equals(s)){
			System.exit(0);
		}
	}
	
	public void readFile(){
		ta.setText("");
		FileDialog fd = new FileDialog(f,"��",FileDialog.LOAD);
		fd.setVisible(true);
		String path = fd.getDirectory();
		String file = fd.getFile();
		BufferedReader br = null;
		try {
			if(path != null && file != null){
				br = new BufferedReader(new FileReader(path+file));
				String s = br.readLine();
				while(s != null){
					//ta.setText(s);
					ta.append(s+"\n");
					s = br.readLine();
				}	
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void saveFile(){
		FileDialog fd = new FileDialog(f,"����",FileDialog.SAVE);
		fd.setVisible(true);
	}

}
